/**
* project3D Engine
* @author John Sword
* @version 2 - AS3
*/

package engine.geom
{
	
	public class Point3D
	{
		public var x:int;
		public var y:int;
		public var z:int;

		/**
		* Create a new Point3D
		* 
		* @param x the x coordinate
		* @param y the y coordinate
		* @param z the z coordinate
		*/ 	
		public function Point3D ( x:int = 0, y:int = 0, z:int = 0)
		{
			this.x = x || 0;
			this.y = y || 0;
			this.z = z || 0;
		}

		public function subtract ( p:Point3D ) : Point3D
		{
			var a:Point3D = new Point3D();
			a.x = x - p.x;
			a.y = y - p.y;
			a.z = z - p.z;
			return a;
		}
		
		public function cross ( p:Point3D ) : Point3D
		{
			return new Point3D( y*p.z-p.y*z, z*p.x-p.z*x, x*p.y-p.x*y );
		}

		public function dot ( p:Point3D ) : Number 
		{
			var d:Number = 0;
			d += x * p.x;
			d += y * p.y;
			d += z * p.z;
			return d;
		}

		public function normalize() : Point3D
		{
	    	return scale(1/length());
	  	}
	  	
	  	public function length() : Number
	  	{
   			return Math.sqrt(dot(this));
  		}
  		
  		public function scale ( n:Number ) : Point3D
  		{
		    var a:Point3D = new Point3D();
		    a.x = n*x;
		    a.y = n*y;
		    a.z = n*z;
		    return a;
		}

		public function clone () : Point3D
		{
			return new Point3D ( x, y, z );
		}
		
		public function toString () : String
		{
			return "x: " + x + " y: " + y + " z:" + z;
		}
		
	    /**
	     * <code>set</code> sets the x,y,z values of the Point3D by copying the
	     * supplied Point3D.
	     *
	     * @param vect
	     *            the Point3D to copy.
	     * @return this point
	     */
	    public function set ( p:Point3D ) : Point3D
	    {
	        x = p.x;
	        y = p.y;
	        z = p.z;
	        return this;
	    }
	    
	}

}